<?php
/**
 * basic users model
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	28.01.2009 19:47
 * @rebuild:	28.01.2010 21:16
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */

class basic_users extends model
{
	protected $table = 'core_users';
	protected $passwords_table = 'core_users_passwords';
	
	public function __construct( )
	{
		if( !config::instance( ) ->password_lifetime )
			$this ->passwords_table = null;
		
		parent::__construct( );
	}
	
	function add( $data, $ignore = false )
	{
		$l = $data['login'];
		$p = $data['password'];
		
		$id = parent::add( array( 'login' => $l, 'password' => '', 'state' => '' ) );
		if( !$id ) return null;
		
		$this ->edit_password( $id, $p );
		
		return $id;
	}
	
	function edit( $id, $data )
	{
		unset( $data['password'] );
		
		$sets = $this ->db ->make_set_list( $data );
		if( !$sets ) return false;
		
		$r = $this ->db ->exec( $q = sprintf(
			"UPDATE %s SET %s WHERE id=%d;",
			$this ->table,
			$sets,
			$id
			) );
		bc::log( $r, $q );
		return $r;
	}
	
	function edit_password( $id, $password )
	{
		$r = $this ->db ->exec( $q = sprintf(
			"UPDATE %1\$s SET password = password( concat( `login`, '-%3\$s' ) )%4\$s WHERE id = %2\$d;",
			$this ->table,
			$id,
			$this ->db ->escape_string( $password ),
			$this ->passwords_table ? ', keepmypassword = 0' : ''
			) );
		//bc::log( $r, $q );
		
		if( $this ->passwords_table )
		{
			$d = $this ->get( $id );
			
			$r = $this ->db ->exec( $q = sprintf(
				"INSERT IGNORE %1\$s SET user_id = %2\$d, password = '%3\$s';",
				$this ->passwords_table,
				$id,
				$this ->db ->escape_string( $d['password'] )
				) );
		}
		
		return $r;
	}
	
	function delete( $id )
	{
		$this ->freeze( $id );
	}
	
	
	public function get( $id, $cacheTime = '6H' )
	{
		$r = parent::get( $id, $cacheTime );

		if( !empty( $r ) )
			$r['fullname'] = sprintf( '%s %s', $r['name'], $r['surname'] );
		
		return $r;
	}
	
	public function by( $p = '', $o = null, $l = null, $cacheTime = '12H' )
	{
		$r = parent::by( $p, $o, $l, $cacheTime );
		
		foreach( array_keys( $r ) as $k )
		{
			unset( $r[$k]['password'] );
			if( !empty( $r[$k] ) )
				$r[$k]['fullname'] = sprintf( '%s %s', $r[$k]['name'], $r[$k]['surname'] );
		}
		
		return $r;
	}
	
	
	
	
	public function get_password_changes( $user_id )
	{
		if( !$this ->passwords_table ) return null;
		
		$sql = <<<sql
select * from (
(select tm, `password` from `{$this->passwords_table}` up where user_id={$user_id})
union (select tm, `password` from `{$this->table}` u where id={$user_id})) q
order by tm desc
limit 50;
sql;
		
		$changes = $this ->db ->_list( $sql );
		//bc::log( $changes, 'changes' );
		
		return $changes;
	}
	
	public function keep_password( $user_id, $period )
	{
		if( !$this ->passwords_table ) return null;
		
		$sql = sprintf( "UPDATE %1\$s SET keepmypassword = keepmypassword + %3\$d WHERE id = %2\$d;",
			$this ->table, $user_id, $period );
		
		$r = $this ->db ->exec( $sql );
		
		return $r;
	}
	
	
	function test_loginpass( $l, $p )
	{
		$sql = sprintf(
			"SELECT id FROM %s WHERE login = '%s' AND password = password( concat( login, '-%s' ) );",
			$this ->table,
			$this ->db ->escape_string( $l ),
			$this ->db ->escape_string( $p )
			);
		return $this ->db ->_value( $sql );
	}
	
	function test_logins( $ls )
	{
		if( is_string( $ls ) ) $ls = array( $ls );
		if( !is_array( $ls ) ) return array( );
		
		$ls = array_filter( $ls );
		$_ls = array( '' ); foreach( $ls as $l ) $_ls[] = $this ->db ->escape_string( $l );
		
		$sql = sprintf(
			"SELECT login, id FROM %s WHERE login in ( %s );",
			$this ->table,
			'"'.join('", "',$_ls).'"'
			);
		
		$e = $this ->db ->_list( $sql, 'login' );
		
		$r = array( );
		foreach( $ls as $l )
			$r[$l] = isset( $e[$l] ) ? $e[$l]['id'] : false;
		
		return $r;
	}

	function test_login( $l )
	{
		$sql = sprintf(
			"SELECT count(*) as count FROM %s WHERE login = '%s';",
			$this ->table,
			$this ->db ->escape_string( $l )
			);
		
		return !( $this ->db ->_value( $sql, 'count' ) );
	}

	function test_email( $email )
	{
		$sql = sprintf(
			"SELECT count(*) as count FROM %s WHERE email = '%s';",
			$this ->table,
			$this ->db ->escape( $email )
			);
		
		return !( $this ->db ->_value( $sql, 'count' ) );
	}
	
	function test_id( $id )
	{
		$sql = sprintf( "SELECT count(*) as count FROM %s WHERE id = '%d';", $this ->table, $id );
		return !( $this ->db ->_value( $sql, 'count' ) );
	}
	function check_id( $id )
	{
		return !$this ->testId( $id );
	}
	
	// states
	function freeze( $id )
	{
		return $this ->db ->exec( sprintf( "UPDATE %s SET state = 'frozen' WHERE id=%d;", $this ->table, $id ) );
	}
	
	function newbieate( $id )
	{
		return $this ->db ->exec( sprintf( "UPDATE %s SET state = 'newbie' WHERE id=%d;", $this ->table, $id ) );
	}

	function personate( $id )
	{
		return $this ->db ->exec( sprintf( "UPDATE %s SET state = 'person' WHERE id=%d;", $this ->table, $id ) );
	}
	
}

?>